package com.example.echoewish.ui.my_activity.other_comment_activity;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.echoewish.econstant.Constant;
import com.example.echoewish.econstant.UserInfo;
import com.example.echoewish.pojo.OthersComment;
import com.example.echoewish.repository.CommentMessageRepository;
import com.example.echoewish.util.LogUtil;

import java.io.IOException;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class OtherCommentViewModel extends ViewModel {
    public static  final  int CHANGE_LIST_VIEW=3;
    public static  final  int DELETE_SUCCESS=3;
    public static  final  int DELETE_FAIL=4;

    MutableLiveData<OthersComment> deleteCommentMutableLiveData;
    MutableLiveData<List<OthersComment>> othersCommentMutableLiveData;
    MutableLiveData<String> responseMutableLiveData;

    public OtherCommentViewModel()
    {
        deleteCommentMutableLiveData=new MutableLiveData<>();
        othersCommentMutableLiveData=new MutableLiveData<>();
        responseMutableLiveData=new MutableLiveData<>();
    }

    public void getOtherComment()
    {
        CommentMessageRepository.getInstance().queryOthersCommentByUserId(UserInfo.getInstance().UserId,new GetOtherCommentCallBack());
    }

    public void deleteOtherComment()
    {
        CommentMessageRepository.getInstance().deleteComment(deleteCommentMutableLiveData.getValue(),new DeleteCommentCallBack());
    }

    private class GetOtherCommentCallBack implements Callback<List<OthersComment>>
    {

        @Override
        public void onResponse(Call<List<OthersComment>> call, Response<List<OthersComment>> response) {
            othersCommentMutableLiveData.postValue(response.body());
        }

        @Override
        public void onFailure(Call<List<OthersComment>> call, Throwable t) {
            othersCommentMutableLiveData.postValue(null);
            new LogUtil().w(LogUtil.CALLBACK_TAG,"获取其他人的帖子信息 回调失败"+t);
        }
    }

    private class DeleteCommentCallBack implements Callback<ResponseBody>
    {

        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            try {
                responseMutableLiveData.postValue(response.body().string());
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            new LogUtil().w(LogUtil.CALLBACK_TAG,"删除其他人的评论信息 回调失败"+t);
            responseMutableLiveData.postValue(Constant.HTTPS_GET_CALLBACK_FAIL);
        }
    }
}
